package com.xiezc.dht.model;

import com.xiezc.dht.util.BeException;
import lombok.Data;

/**
 * @author wb-xzc291800
 */
@Data
public class BeContent {

    private int index;
    private byte[] content;

    public BeContent(byte[] content) {
        this.content = content;
        this.index = 0;
    }

    public byte getCurrent() {
        check(index);
        return content[index++];
    }

    public int skip(int step) {
        check(index + step);
        index += step;
        return index;
    }

    public byte[] getBytes(int len) {
        check(index + len);
        byte[] chars = new byte[len];
        System.arraycopy(content, index, chars, 0, len);
        index += len;
        return chars;
    }

    public byte lookCurrent() {
        check(index);
        return content[index];
    }

    public byte lookNext() {
        check(index + 1);
        return content[index + 1];
    }

    private void check(int index) {
        if (index >= content.length || index < 0) {
            throw new BeException("index 位置不对, 无法继续操作!");
        }
    }

}
